package com.example.sharesinfo.manager;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.example.sharesinfo.bean.NewContentBean;
import com.example.sharesinfo.bean.PersonalCenterBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.ui.NewsInfoActivity;
import com.example.sharesinfo.ui.PersonalCenterActivity;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.google.gson.Gson;

import org.json.JSONException;

public class NewsManager {
    private static final int QUERYNEWSSUCCESS = 1001;
    //对象实例化
    private static volatile NewsManager mInstance = null;

    private Context mContext;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case QUERYNEWSSUCCESS:
                    NewContentBean result1 = (NewContentBean) msg.obj;
                    NewsInfoActivity.startActivity(mContext, result1);
                    break;
            }
            return false;
        }
    });
    private NewsManager() {

    }
    public static NewsManager getInstance() {
        if (mInstance == null) {
            synchronized (DialogManager.class) {
                if (mInstance == null) {
                    mInstance = new NewsManager();
                }
            }
        }
        return mInstance;
    }
    /**
     * 查询具体新闻内容
     */
    public void queryNews(int newsId, Context context) {
        mContext = context;
        String url = "http://119.23.221.142/api/information/GetNewsDetail?NewsId="
                + newsId + "&UserId=" + SpUtils.getInstance().getString(Constants.SP_USER_ID, "");
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                NewContentBean bean = new Gson().fromJson(s, NewContentBean.class);
                message.what = QUERYNEWSSUCCESS;
                message.obj = bean;
                mHandler.sendMessage(message);
            }

            @Override
            public void OnFailed(String s) {

            }
        });
    }
}
